﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Entities;
using DevExpress.Web.ASPxEditors;
namespace Stationery.UC
{
    public partial class UC_ProductList_Update : System.Web.UI.UserControl
    {
        BLL_MenuURL bll_MenuURL = new BLL_MenuURL();
        BLL_Product bll_Product = new BLL_Product();
        protected void Page_Load(object sender, EventArgs e)
        {
            //ASPxComboBoxCategory.DataSource = bll_MenuURL.Get_MenuURL_Dynamic(String.Format("TYPE='{0}' AND ISNULL(ID_PARENT,0)<>0", MenuURL.column));
            //ASPxComboBoxCategory.DataBind();
            if (!IsPostBack)
            {
                Session[Utils.SESSION_DATA] = bll_Product.Get_Product_All();
            }
            else
            {
                if (Session[Utils.SESSION_DATA] == null)
                {
                    Session[Utils.SESSION_DATA] = bll_Product.Get_Product_All();
                }
            }
            ASPxGridView1.DataSource = Session[Utils.SESSION_DATA];
            ASPxGridView1.DataBind();
        }

        protected void ASPxTextBoxName_TextChanged(object sender, EventArgs e)
        {

        }

        //protected void ASPxButtonSearch_Click(object sender, EventArgs e)
        //{
        //    string strCondition = "1=1";
        //    if (ASPxTextBoxCode.Text.Trim() != "")
        //    {
        //        strCondition += " AND " + String.Format("CODE = '{0}'", ASPxTextBoxCode.Text.Trim());
        //    }
        //    if (ASPxTextBoxName.Text.Trim() != "")
        //    {
        //        strCondition += " AND " + String.Format("NAME = '{0}'", ASPxTextBoxName.Text.Trim());
        //    }
        //    if (ASPxComboBoxCategory.Value !=null)
        //    {
        //        strCondition += " AND " + String.Format("ID_MENUURL = {0}", ASPxComboBoxCategory.Value.ToString());
        //    }
        //    ASPxGridView1.DataSource = bll_Product.Get_Product_Dynamic(strCondition);
        //    ASPxGridView1.DataBind();
        //}

        protected void ASPxButtonAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(UtilsURL.product_add);
        }

        protected void LinkButtonDelete_Click(object sender, EventArgs e)
        {
            Int64 intID = Convert.ToInt64(((LinkButton)sender).CommandArgument.ToString());
            string strImageDirectory = bll_Product.Lst_Product(bll_Product.Get_Product(intID))[0].IMAGE_DIRECTORY;
            if (strImageDirectory.ToUpper().Contains("HTTP") || strImageDirectory.ToUpper().Contains("WWW"))
            {
            }
            else
            {
                string strFile = Server.MapPath(strImageDirectory);
                if (File.Exists(strFile))
                {
                    File.Delete(strFile);
                }
            }
            bll_Product.DeleteProduct(intID);
            Response.Redirect(Request.RawUrl);
        }
    }
}